﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


/// <summary>
/// Summary description for DAO
/// </summary>
public class DAO : IDao
{
    private static DAO instance;
    private SqlConnection conn;
    private string connectionString = ConfigurationManager.ConnectionStrings["connString"].ToString();

    private DAO()
    {
        conn = new SqlConnection(connectionString);
    }

    public static DAO Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new DAO();
            }
            return instance;
        }
    }

    private void openConn()
    {
        conn.Open();
    }

    private void closeConn()
    {
        conn.Close();
    }

    public List<Customer> getCustomers()
    {
        try
        {
            openConn();
            SqlCommand cmd = new SqlCommand("exec procGetCustomers", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            List<Customer> customers = new List<Customer>();
            while (dr.Read())
            {
                Customer c = new Customer();
                c.CustomerId = dr["customerID"].ToString().Trim();
                c.CompanyName = dr["companyName"].ToString().Trim();
                c.ValidLicense = Convert.ToBoolean(dr["validLicense"]);
                customers.Add(c);
            }
            return customers;

        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            closeConn();
        }
    }

    public void updateCustomer(Customer c)
    {
        try
        {
            openConn();
            SqlCommand cmd = new SqlCommand("procUpdateLicense", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            SqlParameter parm1 = cmd.Parameters.Add("@customerID", SqlDbType.VarChar, 12);
            parm1.Value = c.CustomerId.ToString().Trim();
            SqlParameter parm2 = cmd.Parameters.Add("@validLicense", SqlDbType.Bit);
            parm2.Value = c.ValidLicense;
            cmd.ExecuteNonQuery();
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            closeConn();

        }
    }


}


